package com.zking.myservlet.order;

import com.zking.dao.OrderDao;
import com.zking.entity.Order;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/manage/OrderList")
public class OrderList extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        String tmp = req.getParameter("pageIndex");
        String id = req.getParameter("searchId");
        String name = req.getParameter("searchName");

        // 用户没有通过订单号搜索，则eo_id=-1
        int eo_id = -1;
        if(id!=null && !id.isEmpty()){
            eo_id = Integer.parseInt(id);
        }

        List<Order>orders;
        int pageIndex = 1;
        int pageSize = 5;
        if(tmp!=null){
            pageIndex = Integer.parseInt(tmp);
        }

        // 存在关键词的情况，对含关键词的信息进行搜索
        int maxSize = OrderDao.getMaxPage(pageSize);
        if((name!=null && !name.isEmpty()) || eo_id!=-1){
            orders = OrderDao.getOrderListByIdAndUserName(eo_id, name, pageIndex, pageSize);
            maxSize = OrderDao.getMaxPageByIdAndName(eo_id, name, pageSize);
        }else{ // 不含的情况
            orders = OrderDao.getOrderList(pageIndex, pageSize);
        }


//        List<Order> orders = OrderDao.getOrderList(pageIndex, pageSize);
        req.setAttribute("list", orders);
        req.setAttribute("maxPage", maxSize);
        req.setAttribute("pageIndex", pageIndex);
        req.setAttribute("searchId", id);
        req.setAttribute("searchName", name);
        req.getRequestDispatcher("order.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
